using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;

namespace YEngineLib.Util
{
    public class YButton
    {
        #region Members
        public enum YButtonState { Up, Down, Pressed, Released };

        public delegate void YButtonHandler(int ownerTouchID, YButtonState buttonState);
        public YButtonHandler   buttonHandler;

        private Vector2         buttonPosition;
        private Rectangle       boundingRect;

        private Texture2D       buttonImage;
        private Color           buttonImageColor = Color.White;

        private Color           buttonTextColor = Color.White;

        private YButtonState    buttonState = YButtonState.Up;
        private int             ownerTouchID = -1;
        #endregion

        #region Properties

        public Vector2 ButtonTextOffset;

        public YText ButtonText;

        public Vector2 ButtonTextShift;

        public Vector2 Position
        {
            get { return buttonPosition; }
            set
            {
                buttonPosition = value;
                MakeBoundingRect();
            }
        }

        public Texture2D Image
        {
            get { return buttonImage; }
            set { buttonImage = value; MakeBoundingRect(); }
        }

        public Color ImageColor
        {
            get { return buttonImageColor; }
            set { buttonImageColor = value; }
        }

        public Rectangle ImageRect
        {
            get;
            set;
        }
        
        #endregion

        public void Init(Vector2 btnPos, Texture2D btnImage, Rectangle? source, YText buttonText)
        {
            buttonPosition = btnPos;
            buttonImage = btnImage;

            ButtonText = buttonText;

            if (source.HasValue)
                ImageRect = source.Value;

            MakeBoundingRect();
        }

        private void MakeBoundingRect()
        {
            int w = buttonImage.Width;
            int h = buttonImage.Height;

            if (ImageRect.IsEmpty == false)
            {
                w = ImageRect.Width;
                h = ImageRect.Height;
            }

            boundingRect = new Rectangle((int)buttonPosition.X, (int)buttonPosition.Y,
                                        w, h);
        }

        public void Reset()
        {
            ownerTouchID = -1;
            buttonState = YButtonState.Up;
        }

        public virtual void Input(TouchLocation touchLocation)
        {
            if (touchLocation.State == TouchLocationState.Invalid)
                ownerTouchID = -1;

            buttonState = YButtonState.Up;

            // if doesn't have an owner
            if (ownerTouchID == -1)
            {
                // check for collision
                if (CheckCollision(touchLocation.Position))
                {
                    // if so, owner is this
                    ownerTouchID = touchLocation.Id;
                    buttonState = YButtonState.Pressed;
                }
            }
            // if has an owner and it is this
            else if (touchLocation.Id == ownerTouchID)
            {
                // check for collision
                if (CheckCollision(touchLocation.Position))
                {
                    buttonState = YButtonState.Down;

                    if (touchLocation.State == TouchLocationState.Released)
                    {
                        buttonState = YButtonState.Released;
                        ownerTouchID = -1;
                    }
                }
                else
                {
                    // if not colliding, no owner, back to UP state
                    ownerTouchID = -1;
                    buttonState = YButtonState.Up;
                }
            }

            if (buttonHandler != null)
                buttonHandler(ownerTouchID, buttonState);
        }

        public bool CheckCollision(Vector2 touchPos)
        {
            Point touchPoint = new Point((int)touchPos.X, (int)touchPos.Y);

            return boundingRect.Contains(touchPoint);
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(buttonImage, Position, ImageRect, buttonImageColor);

            if (ButtonText != null)
                ButtonText.Draw(spriteBatch, Position + ButtonTextOffset + ButtonTextShift, 1.0f);
        }
    }
}
#endif